package org.sws.domain.activity.service.partake;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.sws.domain.activity.model.aggregate.CreatePartakeOrderAggregate;
import org.sws.domain.activity.model.entity.ActivityEntity;
import org.sws.domain.activity.model.entity.ActivityPartakeEntity;
import org.sws.domain.activity.model.entity.UserRaffleOrderEntity;
import org.sws.domain.activity.model.valobj.ActivityStateVO;
import org.sws.domain.activity.repository.IActivityRepository;
import org.sws.types.enums.ResponseCode;
import org.sws.types.exception.AppException;

import java.util.Date;

/**
 * @Author sws
 * @Date 2025/3/18 21:19
 * @description: 抽奖单服务抽象类 抽象类规定业务流程，具体实现叫给子类
 */
@Slf4j
@RequiredArgsConstructor
public abstract class AbstractActivityPartakeServiceService implements IActivityPartakeService {

    protected final IActivityRepository activityRepository;

    @Override
    public UserRaffleOrderEntity createActivityPartake(ActivityPartakeEntity activityPartakeEntity) {
        // 参数校验
        String userId = activityPartakeEntity.getUserId();
        Long activityId = activityPartakeEntity.getActivityId();
        if(null == userId || null == activityId){
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }
        ActivityEntity activityEntity = activityRepository.queryRaffleActivityByActivityId(activityId);
        if(null == activityEntity){
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }
        // 活动状态、时间校验
        if(activityEntity.getState() != ActivityStateVO.open){
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }
        Date currentTime = new Date();
        if(activityEntity.getBeginDateTime().after(currentTime) || activityEntity.getEndDateTime().before(currentTime)){
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(), ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }
        // 查询是否存在未使用的活动单，如果存在则直接返回
        UserRaffleOrderEntity unUsedUserRaffleOrder = activityRepository.queryUnUsedUserRaffleOrder(userId, activityId);
        if(null != unUsedUserRaffleOrder){
            return unUsedUserRaffleOrder;
        }
        // 额度账户过滤，返回账户构建对象
        CreatePartakeOrderAggregate createPartakeOrderAggregate = this.doFilterAndBuildAccount(userId, activityId, currentTime);

        UserRaffleOrderEntity userRaffleOrder = this.buildUserRaffleOrder(userId, activityId, currentTime);
        createPartakeOrderAggregate.setUserRaffleOrderEntity(userRaffleOrder);

        // 数据库更新额度账户 事务操作
        activityRepository.saveCreatePartakeOrderAggregate(createPartakeOrderAggregate);
        // 返回创建用户抽奖单
        return userRaffleOrder;
    }



    protected abstract CreatePartakeOrderAggregate doFilterAndBuildAccount(String userId, Long activityId, Date currentTime);

    protected abstract UserRaffleOrderEntity buildUserRaffleOrder(String userId, Long activityId, Date currentTime);


}
